package com.octopus.yangj.customized.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.octopus.kernal.controller.AbstractCustomizedController;
import com.octopus.kernal.controller.SearchOperator;
import com.octopus.yangj.generated.controller.SearchEventCategoryCommand;
import com.octopus.yangj.generated.model.EventCategoryStruct;
import com.octopus.yangj.generated.service.EventCategoryService;
import com.octopus.yangj.util.EventCategoryLevel;

@Controller
@RequestMapping("/event_category/x")
public class CustomizedEventCategoryController extends AbstractCustomizedController<EventCategoryStruct, SearchEventCategoryCommand, Long>{
	
	@Autowired
	private EventCategoryService categoryServcie;
	
	@RequestMapping(value = "/view_sub.o", method = RequestMethod.GET)
	public String viewSub(Model model, HttpServletRequest request, SearchEventCategoryCommand searchCommand,
	        @RequestParam
	        Long id) {
    	if(!SecurityUtils.getSubject().isPermitted("event_category:query")){
    		return "redirect:/common/unauthorized.o";
    	}
		return "redirect:/event_category/query.o?pid=" + id;
	}
	
	@RequestMapping(value = "/view_up.o", method = RequestMethod.GET)
	public String viewUp(Model model, HttpServletRequest request, SearchEventCategoryCommand searchCommand) {
    	if(!SecurityUtils.getSubject().isPermitted("event_category:query")){
    		return "redirect:/common/unauthorized.o";
    	}
    	String id = request.getParameter("id");
    	EventCategoryStruct category = StringUtils.isBlank(id)?null:categoryServcie.getEntity(Long.valueOf(id));
    	Long pid = 0l;
    	if(category != null && category.getParent() != null && category.getParent().getParent() != null){
    		pid = category.getParent().getParent().getId();
    	}
		return "redirect:/event_category/query.o?pid=" + pid;
	}
	
	@Override
	public String query(Model model, HttpServletRequest request, SearchEventCategoryCommand searchCommand) {
        if (searchCommand == null) {
            searchCommand = new SearchEventCategoryCommand();
        }
        if(searchCommand.getEntity() == null)
        	searchCommand.setEntity(new EventCategoryStruct());
        if(searchCommand.getOperator() == null)
        	searchCommand.setOperator(new EventCategoryStruct());
		String pid = request.getParameter("pid");
		if(StringUtils.isNotBlank(pid)){
			EventCategoryStruct parent = categoryServcie.getEntity(Long.valueOf(pid));
			if(parent == null){
				parent = new EventCategoryStruct();
				parent.setLevel(EventCategoryLevel.FIRST.ordinal() - 1);
				parent.setId(null);
			}
	        searchCommand.getEntity().setLevel(parent.getLevel() + 1);
	        searchCommand.getOperator().setLevel(SearchOperator.EQUAL.ordinal());
	        if(parent.getId() != null){
		        searchCommand.getEntity().setParentId(parent.getId());
		        searchCommand.getOperator().setParentId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
	        }
	        request.getSession().setAttribute("event_categoryCommand", searchCommand.getEntity());
	        request.getSession().setAttribute("event_categoryOperator", searchCommand.getOperator());
	        return "redirect:/event_category/query.o?pager.offset=0";
		}
		return null;
	}

}
